﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS_BackStage.Model.DbModel;
using HANS.DataBaseTool;

namespace HANS_BackStage
{
   public  class BackStageCacheHelper
    {



        /// <summary>
        /// 所有角色
        /// </summary>
        public static List<Backstage_role> AllRoles {
            get {
                Msg msg = new Msg();
                return HANS.Cache.HANSCache.CommCache<List<Backstage_role>>(BackStageCacheKey.AllRoles, () =>
                {
                    return HANS_BackStage.Impl.RoleServices.All();
                },msg,new TimeSpan(0,0,30,0));
            }
            internal set { }
        }

        public static List<Backstage_permission> ALlPermissions {
            get
            {
                Msg msg = new Msg();
                return HANS.Cache.HANSCache.CommCache<List<Backstage_permission>>(BackStageCacheKey.ALlPermissions, () =>
                {
                    return HANS_BackStage.Impl.Role_PermiServices.GetAllPermissions();
                }, msg, new TimeSpan(0, 0, 30, 0));
            }
            internal set { }
        }



        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static  void clearCache() {
            BackStageCacheKey cacheKey = new BackStageCacheKey();
            foreach (var item in cacheKey.keyList)
            {
                HANS.Cache.HANSCache.RefreshCache(item);
            } 
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="key"></param>
        public static void clearCache(string key) {
            HANS.Cache.HANSCache.RefreshCache(key);
        }
    }

    public class BackStageCacheKey {

        public List<string> keyList;
        public BackStageCacheKey() {
            keyList = new List<string>();
            keyList.Add(ALlPermissions);
            keyList.Add(AllRoles);
        }

        /// <summary>
        /// 所有权限
        /// </summary>
        public static string ALlPermissions = "ALlPermissions";
        /// <summary>
        /// 所有角色
        /// </summary>
        public static string AllRoles = "AllRoles";
    }
}
